package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.ReduceStaff;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.dataHelper.utils.ExcelData;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ReduceStaffExcel extends actionBase {
    public void go(List<Long> serverList) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<Future> futureList = new ArrayList<>();
        String dirPath = System.getProperty("user.dir") + File.separator;
        ExcelData excelData = new ExcelData(dirPath + "inExcel" + File.separator + "moneyCard.xlsx", "总表");
        Map<String, Map<String, String>> excelMap = excelData.getExcelData();
        Map<String, Map<String, Map<String, String>>> dataMap = new HashMap<>();
        for (Map.Entry<String, Map<String, String>> entry : excelMap.entrySet()) {
            Map<String, String> value = entry.getValue();
            String serverId = value.get("服务器id");
            String userid = value.get("userid");
            String itemId = value.get("道具ID");
            String num = value.get("数量");
            if (!dataMap.containsKey(serverId)) {
                dataMap.put(serverId, new HashMap<>());
            }
            Map<String, Map<String, String>> userMap = dataMap.get(serverId);
            if (!userMap.containsKey(userid)) {
                userMap.put(userid, new HashMap<>());
            }
            Map<String, String> map = userMap.get(userid);
            map.put(itemId, num);

        }
        GameServer.getLogger().error("dataMap Size {}",dataMap.size());
//        activityIdStr = "9888885";
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                Future future = executorService.submit(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        GameServer testGameServer = new GameServer(serverInfo);
                        ReduceStaff log = new ReduceStaff(testGameServer);
                        Map<String, Map<String, String>> map = dataMap.get(String.valueOf(serverId));
                        log.go(serverId, map);
                        return true;
                    }
                });
                futureList.add(future);
            }
        }
        isResult(futureList);
//            DataMgr.makeExcelData2Txt("");
//            DataMgr.outText("活动商城购买"+activityId);
        DataMgr.makeExcelData2Txt("");
        DataMgr.outText("活动商城购买");
    }
}
